package java111_2;

import java.util.*;

public class Day36 {
    //https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1?tpld=21232&ru=/exam/oj
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String s = "" + n;
            int[] arr = new int[10];
            StringBuilder sb = new StringBuilder();
            for(int i = s.length() - 1; i >= 0; i--){
                if(arr[s.charAt(i) - '0'] == 0){
                    sb.append(s.charAt(i));
                }
                arr[s.charAt(i) - '0']++;
            }
            if(sb.charAt(0) == '0'){
                sb.deleteCharAt(0);
            }
            System.out.println(sb);
        }
    }


    //https://www.nowcoder.com/practice/4c0419eb07c840ca8402e4f2a52cfd49?tpId=308&tqId=40489&ru=/exam/oj
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        PriorityQueue<Long> heap = new PriorityQueue<>();
        while(n-- > 0){
            long tmp = in.nextLong();
            heap.offer(tmp);
        }
        long res = 0;
        while(heap.size() > 1){
            long num1 = heap.poll(), num2 = heap.poll();
            long sum = num1 + num2;
            res += sum;
            heap.offer(sum);
        }
        System.out.println(res);
    }


    //https://www.nowcoder.com/practice/0a8bbf8b9b5b4280957849ef4f240f07?tpId=230&tqId=38957&ru=/exam/oj
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long res = 0;
        String str = in.next();
        int[] fx = new int[26];
        // fx[str.charAt(i) - 'a'] 更新前：表示[0, i-1]区间内有多少个 _x, 更新后：表示[0, i]区间内有多少个 _x
        int[] gx = new int[26];
        // gx[str.charAt(i) - 'a'] 更新前：表示[0, i-1]区间内有多少个 x, 更新后：表示[0, i]区间内有多少个 x
        int[] dp = new int[n+1];
        // dp[i]表示以 i 位置为结尾的子序列中有多少个 -xx;
        for (int i = 0; i < n; i++) {
            int index = str.charAt(i) - 'a';
            dp[i] = fx[index];
            fx[index] += i - gx[index];
            gx[index]++;
            res += dp[i];
        }
        System.out.println(res);
    }
}
